package view;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import model.ActivationDetails;
import model.LoginDetails;
import model.UserSession;

public class ActivatePanel extends JPanel implements ActionListener {

	private ClientFrame frame;
	private JLabel activationLabel;
	private JTextField activationTextField;
	private JButton activationButton;
	private LoginDetails loginDetails;

	public ActivatePanel(ClientFrame f, LoginDetails loginDetails) {
		frame = f;
		this.loginDetails = loginDetails;
		this.setBounds(0, 0, frame.getWidth(), frame.getHeight());
		this.setBackground(Color.WHITE);
		this.setLayout(null);

		activationLabel = new JLabel("Enter the activation number: ");
		activationLabel.setBounds(200, 150, 300, 20);
		this.add(activationLabel);

		activationTextField = new JTextField();
		activationTextField.setBounds(400, 150, 120, 20);
		this.add(activationTextField);

		activationButton = new JButton("Activate");
		activationButton.setBounds(200, 200, 90, 20);
		activationButton.addActionListener(this);
		this.add(activationButton);

		this.repaint();

		frame.setContentPane(this);

	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == activationButton) {

			try {
				Socket connectionSocket = new Socket("127.0.0.1", 4444);
				ObjectOutputStream objectOut = new ObjectOutputStream(
						connectionSocket.getOutputStream());
				ObjectInputStream objectIn = new ObjectInputStream(
						connectionSocket.getInputStream());

				ActivationDetails activationDetails = new ActivationDetails();
				activationDetails.setActivationCode(activationTextField
						.getText());
				activationDetails.setLoginDetails(this.loginDetails);
				objectOut.writeObject(activationDetails);
				objectOut.flush();
				String message = (String) objectIn.readObject();
				if (!message.equals("user activated")) {
					JOptionPane
							.showMessageDialog(this,
									"Sorry incorrect activation code. Please check your mail and try again.");
					return;
				} else {
					objectOut.writeObject("get user session");
					objectOut.flush();
					UserSession userSession = (UserSession) objectIn
							.readObject();
					objectOut.flush();
					this.frame.setUserSession(userSession);

					objectOut.writeObject("get voting status");
					objectOut.flush();
					message = (String) objectIn.readObject();
					connectionSocket.close();
					if (message.startsWith("Sorry, voting hasn't started yet.")) {
						this.frame.goToDiplayMessagePanel(message);
					} else if (message.startsWith("you have voted for")) {
						this.frame.goToDiplayMessagePanel(message);
					}
					if (message.equals("Sorry, voting has closed.")) {
						this.frame.goToViewResultsPanel();
					} else if (message.equals("user hasn't voted")) {
						this.frame.goToVotePanel();
					}
				}
			} catch (Exception ex) {
				ex.printStackTrace();
			}

			return;
		}

	}
}
